<template>
	<view class="user_content">
		<view class="user_list">
			<view class="user_item" v-if="editType == 'userName'">
				<text>Name：</text>
				<input type="text" v-model="userList.name" />
			</view>
			<view class="user_item" v-if="editType == 'tel'">
				<text>Mobile：</text>
				<input type="text" v-model="userList.tel" maxlength="15" />
			</view>
			<view class="user_item" v-if="editType == 'email'">
				<text>Email：</text>
				<input type="text" v-model="userList.email" />
			</view>
			<button class="save_btn" @tap="save">Save</button>
		</view>
	</view>
</template>

<script>
import api from '../../api/userInfoApi.js';
import otherApi from '../../api/otherApi.js'
import utils from '../../api/utils.js'

export default {
	data() {
		return {
			userList: {},
			userId: '',
			userAccount: '',
			userType: '',
			token: '',
			editType: ''
		};
	},
	methods: {
		async save() {
			let _this = this;
			if (_this.userList.name == '') {
				uni.showToast({
					title: 'Pleae input Name！',
					icon: 'none',
					duration: 2000
				});
				return false;
			}
			if (_this.userList.tel == '') {
				uni.showToast({
					title: 'Pleae input Mobile！',
					icon: 'none',
					duration: 2000
				});
				return false;
			}
			if (_this.userList.email == '') {
				uni.showToast({
					title: 'Pleae input Email！',
					icon: 'none',
					duration: 2000
				});
				return false;
			}
			
			let obj = {}
			let reqPath = ''
			
			if (_this.userType == '家长') {
				obj = {
					userName: _this.userList.name,
					parentEmail: _this.userList.email,
					parentPhone: _this.userList.tel,
					userId: _this.userId,
					userAccount: _this.userAccount
				}
				reqPath = 'user/editParent'
			} else if (_this.userType == '监护人') {
				obj = {
					userName: _this.userList.name,
					guardianEmail: _this.userList.email,
					guardianPhone: _this.userList.tel,
					userId: _this.userId,
					userAccount: _this.userAccount
				}
				reqPath = 'user/editGuardian'
			} else if (_this.userType == '学生') {
				obj = {
					userName: _this.userList.name,
					stuEmail: _this.userList.email,
					stuPhoneHome: _this.userList.tel,
					userId: _this.userId,
					userAccount: _this.userAccount
				}
				reqPath = 'user/editStu'
			}

			let data = await api.editParent(reqPath, obj, _this.token);
			if (data.head.status) {
				let userInfo = {}
				uni.getStorage({
					key: 'userInfo',
					success(res) {
						userInfo = res.data
					}
				})
				
				if (_this.editType == 'userName') {
					userInfo.userName = _this.userList.name
				} else if (_this.editType == 'tel' && userInfo.userType == '监护人') {
					userInfo.guardianPhone = _this.userList.tel
				} else if (_this.editType == 'tel' && userInfo.userType == '家长') {
					userInfo.parentPhone = _this.userList.tel
				} else if (_this.editType == 'tel' && userInfo.userType == '学生') {
					userInfo.stuPhoneHome = _this.userList.tel
				} else if (_this.editType == 'email' && userInfo.userType == '监护人') {
					userInfo.guardianEmail = _this.userList.email
				} else if (_this.editType == 'email' && userInfo.userType == '家长') {
					userInfo.parentEmail = _this.userList.email
				} else if (_this.editType == 'email' && userInfo.userType == '学生') {
					userInfo.stuEmail = _this.userList.email
				}
				
				uni.removeStorage({
					key: 'userInfo',
					success(res) {
						console.log(res)
					}
				})
				
				uni.setStorage({
					key: 'userInfo',
					data: userInfo
				})
				
				uni.showToast({
					title: 'Success！',
					duration: 1000
				});
				setTimeout(() => {
					uni.redirectTo({
						url: "./my"
					})
				}, 1200)
			} else {
				uni.showToast({
					title: data.head.msg,
					icon: 'none',
					duration: 1000
				});
			}
		}
	},
	onLoad(options) {
		this.editType = options.type
		console.log(this.editType)
	},
	async onShow() {
		let _this = this;
		uni.getStorage({
			key: 'token',
			success(res) {
				_this.token = res.data
			}
		})
		uni.getStorage({
			key: 'userInfo',
			success(res) {
				console.log(res);
				_this.userType = res.data.userType
				if (res.data.userType == '家长') {
					_this.userList = {
						type: res.data.userType,
						name: res.data.userName,
						tel: res.data.parentPhone,
						email: res.data.parentEmail
					};
				} else if (res.data.userType == '监护人') {
					_this.userList = {
						type: res.data.userType,
						name: res.data.userName,
						tel: res.data.guardianPhone,
						email: res.data.guardianEmail
					}
				} else if (res.data.userType == '学生') {
					_this.userList = {
						type: res.data.userType,
						name: res.data.userName,
						tel: res.data.stuPhoneHome,
						email: res.data.stuEmail
					}
				}
				_this.userId = res.data.userId;
				_this.userAccount = res.data.userAccount
			}
		});
		if (utils.userType == '学生' && utils.platform == 'android') {
			uni.getLocation({
				type: 'wgs84',
				geocode: true,
				async success(res) {
					let Position = {};
					Position = {
						address: (res.address.country + res.address.province + res.address.city + res.address.district + res.address.street + res.address.streetNum).replace(
							/undefined/g,
							''
						),
						latitude: res.latitude,
						longitude: res.longitude
					};
					let savePosition = await otherApi.savePosition('position/savePosition', Position, _this.token);
				}
			});
			setInterval(async () => {
				let Position = {};
				uni.getLocation({
					type: 'wgs84',
					geocode: true,
					async success(res) {
						let Position = {};
						Position = {
							address: (res.address.country + res.address.province + res.address.city + res.address.district + res.address.street + res.address.streetNum).replace(
								/undefined/g,
								''
							),
							latitude: res.latitude,
							longitude: res.longitude
						};
						await otherApi.savePosition('position/savePosition', Position, _this.token);
					}
				});
			}, 900000);
		}
	}
};
</script>

<style scoped>
.user_content {
	width: 100%;
}

.user_list {
	width: 100%;
	padding: 0 20px;
	box-sizing: border-box;
	margin-top: 25px;
}

.user_item {
	width: 100%;
	padding: 20px 5px;
	border-bottom: solid 1px #dfdfdf;
	display: flex;
	justify-content: flex-start;
	align-items: center;
	margin-bottom: 20px;
	box-sizing: border-box;
}

.user_item:first-child {
	justify-content: space-between !important;
}

.user_item text {
	color: #666;
	font-size: 18px;
}

.user_item input {
	color: #333;
	font-size: 18px;
}

.save_btn {
	margin-top: 100px;
	width: 100%;
	height: 50px;
	letter-spacing: 3px;
	color: #666;
}
</style>
